//
//  ZWJNetworkManager.m
//  Free
//
//  Created by 张炜健 on 2018/4/28.
//  Copyright © 2018年 张炜健. All rights reserved.
//

#import "ZWJNetworkManager.h"
#import "ZWJNetworkApi.h"
#import "ZWJUploadImageApi.h"
#import "FilesHeader.h"
@implementation ZWJNetworkManager


+(void)requestWithRequestData:(NSDictionary *)requestData WithUrl:(NSString *)url WithWithCompletionBlockWithSuccess:(successBlock)success failure:(block)failure{
    __weak typeof(self) weakSelf = self;
    ZWJNetworkApi *api = [[ZWJNetworkApi alloc]initWithRequestData:requestData WithUrl:url];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
//        NSLog(@"%@22222",request.responseJSONObject);
//        NSLog(@"%@44444",request.responseObject);
        //转成json格式
        NSError *errorJson;
        NSDictionary *requestDic = [NSJSONSerialization JSONObjectWithData:request.responseObject options:kNilOptions error:&errorJson];
//        NSLog(@"-----%@",requestDic);
        
        //返回code == 400 走自动登录
        if ([[requestDic objectForKey:Code]intValue] == 400 ||requestDic==nil) {
            if ([DGUser objectForKey:UserName] == nil||[[DGUser objectForKey:UserName] isEqualToString:@""]) {//首次进来
                
            }else{
                if (![[DGUser objectForKey:UserName] isEqualToString:@""]){
                    NSMutableDictionary *parameterDict = [NSMutableDictionary dictionary];
                    //从偏好设置中读取
                    //存入偏好设置
                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    NSString *usernameStr = [userDefaults objectForKey:@"username"];
                    NSString *passwordStr = [userDefaults objectForKey:@"password"];
                    [parameterDict setObject:@"userVo" forKey:@"loginType"];
                    [parameterDict setObject:usernameStr forKey:@"username"];
                    [parameterDict setObject:passwordStr forKey:@"password"];
                    [parameterDict setObject:@"1" forKey:@"appCode"];
                    
                    ZWJNetworkApi *loginApi = [[ZWJNetworkApi alloc]initWithRequestData:parameterDict WithUrl:@""];
                    [loginApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                        NSError *errorJson;
                        NSDictionary *requestDic = [NSJSONSerialization JSONObjectWithData:request.responseObject options:kNilOptions error:&errorJson];
                        if ([[requestDic objectForKey:Code]intValue] == 200) {
                            
                            [weakSelf requestWithRequestData:requestData WithUrl:url WithWithCompletionBlockWithSuccess:^(NSDictionary *dic) {
                                success(dic);
                            } failure:^(id error) {
                                
                            }];
                        }
                    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                        NSLog(@"自动登录失败");
                    }];
                }
            }
        }else{
            success(requestDic);
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"%@",request.error);
        failure(request.error);
    }];

}

+(void)uloadImage:(UIImage *)image WithWithCompletionBlockWithSuccess:(successBlock)success failure:(block)failure{
    ZWJUploadImageApi *api = [[ZWJUploadImageApi alloc]initWithImage:image];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        //转成json格式
        NSError *errorJson;
        NSDictionary *requestDic = [NSJSONSerialization JSONObjectWithData:request.responseObject options:kNilOptions error:&errorJson];
        //        NSLog(@"-----%@",requestDic);
        success(requestDic);
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"%@",request);
        failure(request.error);
    }];
}


@end
